import service from "@service";
import { getShaSign } from "@utils";
import { formatDate } from "@utils";
import { getTokenInfo, setSessionCookie } from "@utils/session";

/**
 * 
 * @param {object} row 操作行
 * @param {Boolean} isAdd 是否是新增
 * @returns 
 */
const getOperatingByRow = (row, isAdd) => {
    let staffStatus = row.staffIdStatus5 || row.staffIdStatus; // 01 正常， 02 停用
    let operating = '0'; // 0:新增 1：修改 2：停用
    if (isAdd) {
        return '0';
    }
    if (staffStatus === '01') {
        return '1';
    } else if (staffStatus === '02') {
        return '2'
    }
    return operating;
}
export default {
    data() {
        return {}
    },
    methods: {
        bindWindowEvent() {
            window.synchronizationCsms = this.synchronizationCsms;
            window.synchronizationInner = this.synchronizationInner;
            window.syncCsmsAndInner = this.syncCsmsAndInner;
        },
        /**
         * sam人员修改或新增保存后自动更新
         * @param {object} data 当前修改的数据
         * @param {object} row 操作当前行数据
         */
        syncCsmsAndInner(data, row) {
            let isAdd = !row;
            let comprehensiveYn = isAdd ? false : String(row.comprehensiveYn) === '1';
            let internetYn = isAdd ? false : String(row.internetYn) === '1';
            console.log(data, "----syncCsmsAndInnerdata");
        },
        // 同步综合
        async synchronizationCsms(row, successCb, errorCb) {
            let loading = await this.$store.dispatch('global/loading');
            let isAdd = String(row.comprehensiveYn) !== '1';

            let params = {
                staffId: row.staffId,
                roleId: '001',
                // operating: String(row.comprehensiveYn) === '1' ? '1' : '0',  // 0:新增 1：修改 2：停用
                operating: getOperatingByRow(row, isAdd)
            }
            let token = getTokenInfo();
            let result = await service('comprehensiveStaffInfo', {
                data: params,
                headers: {
                    accesstoken: token.access_token,
                    token: token.access_token
                }
            }).finally(e => {
                loading.close();
            })

            if (result) {
                if (isAdd) {
                    this.updateSamInfo(row, {
                        comprehensiveYn: "1"
                    }, successCb, errorCb)
                } else {
                    successCb && successCb();
                }
            }
        },
        // 同步互联网
        async synchronizationInner(row, successCb, errorCb) {

            let loading = await this.$store.dispatch('global/loading');
            let isAdd = String(row.internetYn) !== '1';

            let params = {
                staffId: row.staffId,
                roleId: '02',
                operating: getOperatingByRow(row, isAdd)
                // operating: String(row.internetYn) === '1' ? '1' : '0',  // 0:新增 1：修改 2：停用
            }
            let token = getTokenInfo();

            let serviceName = 'internetStaffInfo';
            if(params.operating === '1') {
                serviceName = 'updateInternetStaffInfo'
            }

            let result = await service(serviceName, {
                data: params,
                headers: {
                    accesstoken: token.access_token,
                    token: token.access_token,
                    staffId: this.staffId
                }
            }).finally(e => {
                loading.close();
            })

            if (result) {
                if(isAdd) {
                    this.updateSamInfo(row, {
                        internetYn: "1"
                    }, successCb, errorCb)
                }else {
                    successCb&&successCb();
                }
            }
        },
        async updateSamInfo(row, data, successCb, errorCb) {
            let params = {
                staffId: row.staffId,
                staffName: row.staffName,
                staffState: row.staffState,
                mobilePhone: row.mobilePhone,
                staffIdStatus: row.staffIdStatus,
                orgaId: row.orgaId,
                postId: row.postId,
                emailAddress: row.emailAddress,
                idCardNo: row.idCardNo,
                channelId: row.channelId,
                defaultServiceType: row.defaultServiceType,
                prsnchnltypecd: row.prsnChnlTypeCd,
                internetYn: row.internetYn,
                comprehensiveYn: row.comprehensiveYn,
            }
            let result = await service('updateStaffInfo', {
                data: Object.assign(params, (data || {}))
            });
            if (result) {
                successCb && successCb();
            }
        }
    }
}